package pers.baiban.sqltoword.service.impl;

import pers.baiban.sqltoword.mapper.TablesMapper;
import pers.baiban.sqltoword.model.Tables;
import pers.baiban.sqltoword.service.ColumnsService;
import pers.baiban.sqltoword.service.TablesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TablesServiceImpl implements TablesService {

    @Autowired
    private TablesMapper baseMapper;

    @Autowired
    private ColumnsService columnsService;

    @Override
    public Tables queryTables(String tableName) {
        return baseMapper.queryTables(tableName);
    }

    @Override
    public Tables queryFullTables(String tableName) {
        Tables tables = queryTables(tableName);
        tables.setColumns(columnsService.queryColumns(tableName));
        return tables;
    }

    @Override
    public List<Tables> listFullTables() {
        return baseMapper.list().stream()
                .peek(it -> it.setColumns(columnsService.queryColumns(it.getTableName())))
                .collect(Collectors.toList());
    }
}